package com.cn.yz.service.sysauthority.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.cn.yz.common.constant.DList;
import com.cn.yz.common.exception.BusinessException;
import com.cn.yz.common.exception.ErrMessage;
import com.cn.yz.common.page.Page;
import com.cn.yz.common.page.PagedData;
import com.cn.yz.common.utils.EncryptUtil;
import com.cn.yz.service.sysauthority.config.SystemConfigBean;
import com.cn.yz.service.sysauthority.mapper.SysUserMapper;
import com.cn.yz.service.sysauthority.model.SysUser;
import com.cn.yz.service.sysauthority.service.SysUserService;

@Service
@EnableConfigurationProperties({SystemConfigBean.class})
public class SysUserServiceImpl implements SysUserService {
	
	private static Logger LOG = LoggerFactory.getLogger(SysUserServiceImpl.class);
	
	@Autowired
	SysUserMapper sysUserMapper;
	
	@Autowired
	SystemConfigBean systemConfigBean;

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.SysUserService#selectByPrimaryKey(java.lang.Integer)
	 */
	@Override
	public SysUser selectByPrimaryKey(Integer id) {
		if(id == null) {
			throw new BusinessException(ErrMessage.ID_MUST);
		}
		return sysUserMapper.getByPrimaryKey(id);
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.SysUserService#addSysUser(com.cn.yz.service.sysauthority.model.SysUser)
	 */
	@Override
	public void addSysUser(SysUser sysUser) {
		if(StringUtils.isEmpty(sysUser.getAccount())) {
			throw new BusinessException(ErrMessage.ACCOUNT_MUST);
		}
		
		String passwordEncrypt = sysUser.getAccount();
		if(StringUtils.isEmpty(passwordEncrypt)) {
			throw new BusinessException(ErrMessage.PASSWORD_MUST);
		}
//		LOG.info("saltDigit------>"+saltDigit);
		String salt = EncryptUtil.getSalt(systemConfigBean.getSaltLength());
		sysUser.setSalt(salt);
			
		try {
			sysUser.setPassword(EncryptUtil.encryptSha1(passwordEncrypt, salt));
			sysUser.setCreateTime(new Date());
			sysUserMapper.saveSelective(sysUser);
		} catch (DuplicateKeyException e) {//acount不能重复
			throw new BusinessException(ErrMessage.ACCOUNT_REPEAT);
		} catch (NoSuchAlgorithmException e) {
			LOG.error("LoginServiceImpl.login ---->"+e.getMessage());
			throw new BusinessException(ErrMessage.PASSWORD_CANNOT_ENCRYPT);
		} catch (UnsupportedEncodingException e) {
			LOG.error("LoginServiceImpl.login ---->"+e.getMessage());
			throw new BusinessException(ErrMessage.PASSWORD_CANNOT_ENCRYPT);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.SysUserService#selectAccount(java.lang.String)
	 */
	@Override
	public SysUser selectSysUserByAccount(String account) {
		if(StringUtils.isEmpty(account)) {
			throw new BusinessException(ErrMessage.ACCOUNT_MUST);
		}
		SysUser sysUser = sysUserMapper.getSysUserByAccount(account);
		if(sysUser == null){
			throw new BusinessException(ErrMessage.RECORD_NON_EXISTENT);
		}
		return sysUser;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.SysUserService#delSysUser(java.lang.Integer)
	 */
	@Override
	public void delSysUser(Integer id) throws BusinessException {
		if(id == null) {
			throw new BusinessException(ErrMessage.ID_MUST);
		}
		SysUser sysUser = sysUserMapper.getByPrimaryKey(id);
		if(sysUser==null){
			throw new BusinessException(ErrMessage.RECORD_NON_EXISTENT);
		}
		if(sysUser.getDeleteFlag()==DList.Attribute.NO){
			throw new BusinessException(ErrMessage.RECORD_CANNOT_DELETE);
		}
		int result = sysUserMapper.removeByPrimaryKey(id);
		if(result == 0){
			throw new BusinessException(ErrMessage.RECORD_NON_EXISTENT);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.SysUserService#modSysUser(com.cn.yz.service.sysauthority.model.SysUser)
	 */
	@Override
	public void modSysUser(SysUser sysUser) {
		if(sysUser.getId() == null) {
			throw new BusinessException(ErrMessage.ID_MUST);
		}
		int result = sysUserMapper.updateByPrimaryKeySelective(sysUser);
		if(result==0){
			throw new BusinessException(ErrMessage.RECORD_NON_EXISTENT);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cn.yz.service.sysauthority.service.SysUserService#selectSysUserList(java.util.Map, com.cn.yz.common.page.Page)
	 */
	@Override
	public PagedData<SysUser> selectSysUserList(Map<String,Object> condition,Page page) {
		List<SysUser> sysUser = sysUserMapper.listSysUserByCondition(page, condition);
		long count = sysUserMapper.countSysUserByCondition(condition);
		return new PagedData<SysUser>(page,sysUser,count);
	}

}
